var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:null,
    goodList:[],
    // 购物车中的商品信息
    product:{},
    index:0,
    pay:["在线支付","现金交易","扫码支付"],
    total:0,
    desk_num:"",
    show:true,
    cart_id:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取用户信息
    this.setData({
      userinfo: app.globalData.userInfo
    })
    // 从缓存中获取桌牌号desk_num
    var desk = wx.getStorageSync('desk');
    if(desk != {}){
      for(var i in desk){
        var desk_num = desk[i];
        this.setData({
          desk_num: desk_num
        })
      }
    }else{
      var desk_num = "";
    }
    if(desk_num >0){
      this.setData({
        show:true
      })
    }else{
      this.setData({
        show: false
      })
    }
   
    // 将app里面的goodList赋值给本页面（购物车中的商品）
    var that = this;
    var list = app.globalData.goodList;
    var cart_id = new Array();
    for(var index in list){
      var cart = list[index].cartid;
      cart_id[index] =cart;
    }
    this.setData({
      goodList: list,
      cart_id:cart_id
    })
    wx.request({
      url: app.globalData.yuming +'index.php/Api/Order/confirmOrder',
      data:{
        m_id:app.globalData.m_id,
        cart_id:cart_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method:"POST",
      success:function(res){
        var goodList = res.data.data.product;
        var total = res.data.data.total
        that.setData({
          product:goodList,
          total:total
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  // 选择支付方式
  bindPickerChange:function(e){
    var value = e.detail.value;
    this.setData({
      index:value
    })
  },
  // 提交订单
  shopSubmit:function(){
    var m_id = app.globalData.m_id;
    var cart_id = this.data.cart_id;
    var desk = parseInt(this.data.desk_num) ;
    if (!/^\d+$/.test(desk)){
      wx.showModal({
        title:"提示",
        content:"请输入正确的桌号",
        showCancel:false
      })
    }else{
    wx.request({
      url: app.globalData.yuming +'index.php/Api/Order/addOrder',
      data:{
          m_id:m_id,
          cart_id:cart_id,
          desk:desk
      },
      header:{
        'content-type': 'application/x-www-form-urlencoded'
      },
      method:"POST",
      success:function(res){
        if(res.data.message == "下单成功"){
          wx.showModal({
            title: "提示",
            content: res.data.message + "请及时付款",
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../list/list',
                })
              }
            }
          })
        }else{
          wx.showModal({
            title: "提示",
            content: res.data.message,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../list/list',
                })
              }
            }
          })
        }
       

      },
      fail:function(res){
        wx.showModal({
          title: "提示",
          content: res.data.message,
          showCancel: false
        })
      }
    })
    }
  },
  // 获取填写的桌号
  deskinput:function(e){
    this.setData({
      desk_num:e.detail.value
    })
  }
})